<template>
  <div>
    <NavBar title="商品列表" />
    <div class="min-bar">
      <div @click="handleOrderSales" :class="['order', 'sale', {
       active: orderStatus.activeIndex === 0
      }]">
        <span>销量</span>
      </div>
      <div @click="handleOrderPrice">
        <span :class="['order', 'price', {
          active: orderStatus.activeIndex === 1
        }]">价格</span>
        <van-icon :class="['arrow', 'up', {
          active: orderStatus.activeIndex === 1 && orderStatus.upDown === 1
        }]" class-prefix="icon" name="shangjiantou" />
        <van-icon :class="['arrow', 'bottom', {
          active: orderStatus.activeIndex === 1 && orderStatus.upDown === 0
        }]" class-prefix="icon" name="xiajiantou" />
      </div>
      <div @click="handleOrderNew">
        <span :class="['order', 'new', {
          active: orderStatus.activeIndex === 2
        }]">新品</span>
        <van-icon :class="['arrow', 'up', {
          active: orderStatus.activeIndex === 2 && orderStatus.upDown === 1
        }]" class-prefix="icon" name="shangjiantou" />
        <van-icon :class="['arrow', 'bottom', {
          active: orderStatus.activeIndex === 2 && orderStatus.upDown === 0
        }]" class-prefix="icon" name="xiajiantou" />
      </div>
    </div>
    <div class="content">
      <div class="items">
        <div class="item" v-for="item in items" :key="item.id" @click="enterDetail(item.id)">
          <img :src="item.pic" alt="">
          <div class="item-info">
            <h5>{{ item.name }}</h5>
            ￥<span>{{ item.originalPrice }}</span>
            <van-icon class="addCart" class-prefix="icon" name="gouwuchetianjia" size="20PX" />
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang='ts' setup name="ItemLists">
import NavBar from '@c/NavBar.vue'
import { ref, reactive, computed, onMounted } from 'vue'
import { fetchList } from '@/api';
import { useRoute, useRouter } from 'vue-router';
const route = useRoute()
const router = useRouter()
//跳转详情
const enterDetail = (id: number) => {
  router.push({
    path: '/details',
    query: {
      id
    }
  })
}
const orderStatus = reactive({
  activeIndex: 0, // 0销量 1 价格 2 新品
  upDown: 0 // 0降序 1 升序
})
//排序
const order = computed(() => {
  let orderSort = 'ordersDown'
  //判断
  switch (orderStatus.activeIndex) {
    case 0:
      orderSort = 'ordersDown'
      break;
    case 1:
      if (orderStatus.upDown === 0) {
        orderSort = 'priceDown'
      } else {
        orderSort = 'priceUp'
      }
      break;
    case 2:
      if (orderStatus.upDown === 0) {
        orderSort = 'addedDown'
      } else {
        orderSort = 'addedUp'
      }
      break;
  }
  return orderSort
})

// 获取商品列表
const items = ref<Array<any>>([])
const fetchItemsByCateId = () => {
  console.log(order.value);

  fetchList({ orderBy: order.value }).then(res => {
    console.log(res);
    if (res.data.code === 0) {
      items.value = res.data.data.result
    }

  })
}

// 按照销量排序
const handleOrderSales = () => {
  orderStatus.activeIndex = 0
  fetchItemsByCateId()
}
//按照价格
const handleOrderPrice = () => {
  // orderStatus.activeIndex = 1
  if (orderStatus.activeIndex !== 1) {
    orderStatus.activeIndex = 1
    orderStatus.upDown = 1
  } else {
    if (orderStatus.upDown === 1) {
      orderStatus.upDown = 0
    } else {
      orderStatus.upDown = 1
    }
  }
  fetchItemsByCateId()
}
//按照新品
const handleOrderNew = () => {
  // orderStatus.activeIndex = 2
  if (orderStatus.activeIndex !== 2) {
    orderStatus.activeIndex = 2
    orderStatus.upDown = 1
  } else {
    if (orderStatus.upDown === 1) {
      orderStatus.upDown = 0
    } else {
      orderStatus.upDown = 1
    }
  }
  fetchItemsByCateId()
}
onMounted(() => {
  fetchItemsByCateId()
})
</script>

<style lang="scss" scoped>
.min-bar {
  width: 100%;
  position: fixed;
  left: 0;
  top: 46px;
  display: flex;
  font-size: 14PX;
  color: #333;
  align-items: center;
  justify-content: space-around;
  background-color: #fff;
  height: 40px;
  z-index: 4;

  .up {
    position: absolute;
    top: -5PX;
    left: 35PX;

  }

  /* .order.active {
    color: #e93323;
  } */

  .active {
    color: #ec1109;
  }

  .bottom {
    position: absolute;
    top: 5PX;
    left: 35PX;

  }
}

.items {
  margin-top: 100px;

  .item {
    height: 110px;
    display: flex;
    padding: 10px;
    background-color: #fff;
    margin-top: 10px;

    img {
      width: 4rem;
    }

    .item-info {
      flex: 1;
      padding: 0 10px;

      h5 {
        width: 5rem;
        line-height: 40px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        padding-top: 5px;
      }

      span {
        font-size: 20PX;
        color: #ec1109;
      }

      .addCart {
        margin-left: 100PX;
      }

      .item-action {
        display: flex;
        justify-content: space-between;

        p {
          font-size: 20PX;
          color: #ec1109;
        }

      }
    }
  }
}
</style>